package com.xn.app.muying.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xn.app.muying.MuYingApplication;
import com.xn.app.muying.R;
import com.xn.app.muying.util.Constants;
import com.xn.app.muying.util.MyAlertDialog;
import com.xn.app.muying.util.ToastUtil;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2016/10/19.
 */
public class YuChanJiSuanActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yuchanjisuan);
        ImageView back = (ImageView) findViewById(R.id.yuchanjisuan_activity_back);
        et = (EditText) findViewById(R.id.yuchanjisuan_activity_et);
        Button btn = (Button) findViewById(R.id.yuchanjisuan_activity_btn);


        back.setOnClickListener(this);
        btn.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.yuchanjisuan_activity_back:
                finish();
                break;

            case R.id.yuchanjisuan_activity_btn:
                float date = Float.parseFloat(et.getText().toString().trim());
                yuchanjisuan(date);

                break;
        }
    }


    private void yuchanjisuan(final float date) {
        MyAlertDialog.ShowDialog(YuChanJiSuanActivity.this, R.layout.dialog_loading);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String tokens = Constants.getStringByName(YuChanJiSuanActivity.this, "yonghu");
                    Type type1 = new TypeToken<HashMap<String, String>>() {
                    }.getType();
                    HashMap<String, String> token = new Gson().fromJson(tokens, type1);
                    Log.i("asdf", "access_token==" + "Bearer " + token.get("access_token"));
                    Request request = new Request.Builder().url("http://apitest.beierbaby.com/api/antenatalcares/predeliverydate?lastMenstruationDate=" + date + "").addHeader("Authorization", "Bearer " + token.get("access_token")).get().build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();

                    int code = response.code();
                    String bodys = response.body().string();
                    Intent intent = new Intent();
                    intent.putExtra("shijian", bodys);
                    setResult(4, intent);
                    finish();

                    Log.i("code====", "code==" + code);
                    Log.i("bodys==", bodys);
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        msg2.obj = bodys;
                        handler.sendMessage(msg2);
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.arg1 = code;
                        msg1.obj = bodys;
                        handler.sendMessage(msg1);
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    ToastUtil.showToast(YuChanJiSuanActivity.this, "设置成功");
                    break;
                case 1:
                    ToastUtil.showToast(YuChanJiSuanActivity.this, "设置失败");
                    break;
            }
        }
    };
}

